package longrise.phone.com.bjjt_henan.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Adapter.PeccancyAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 违章查询
 */
public class SearchPeccancyView extends LFView {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mListView;

    private PeccancyAdapter mAdapter;

    private EntityBean bean;

    private String carno;

    private TextView emptyIV;//无数据时的背景色

    private boolean isLoading = false;// 正在刷新

    private BaseApplication mApplication;

    private TextView countTV;

    private TextView sCountTV;

    private TextView pCountTV;

    public SearchPeccancyView(Context context, EntityBean beans) {
        super(context);
        this.mContext = context;
        this.bean = beans;
        this.mApplication = ((MainActivity) context).getAppData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }


    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_searchpeccancy, null);
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        titleTV.setText(mContext.getResources().getString(R.string.breakrule_search));
        TextView carnumTV = (TextView) mView.findViewById(R.id.peccancy_tv_carnum);
        countTV = (TextView) mView.findViewById(R.id.peccancy_tv_count);
        sCountTV = (TextView) mView.findViewById(R.id.peccancy_tv_scount);
        pCountTV = (TextView) mView.findViewById(R.id.peccancy_tv_money);
        mListView = (PullToRefreshListView) mView.findViewById(R.id.peccancy_lv);
        emptyIV = (TextView) mView.findViewById(R.id.empty_tv);
        initRefreshView();

        mAdapter = new PeccancyAdapter(mContext);
        mListView.setAdapter(mAdapter);

        carno = bean.getString("carno");
        //设置数据
        carnumTV.setText(carno);
    }

    private void initRefreshView() {
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        // 刚拉时的提示
        mListView.getLoadingLayoutProxy().setPullLabel("加载列表数据...");
        // 正在刷新提示
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");
        // 拉倒一定高度的提示
        mListView.getLoadingLayoutProxy().setReleaseLabel("放开以刷新...");
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    searchPeccancy();
                    isLoading = true;
                }
            }
        });
    }

    @Override
    public void refresh() {
        DialogUtils.showDialog(mContext);
        searchPeccancy();
    }

    //违章查询
    private void searchPeccancy() {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        String carCode = bean.getString("identificationnum");
        String carType = bean.getString("cartype");
        String engineno = bean.getString("engineno");
        if (!TextUtils.isEmpty(carType) && carType.length() == 1) {//车辆类型为两位数
            carType = "0" + carType;
        }
        ApiImpI.getInstance(mContext).searchPeccancy(carno, carCode, engineno, carType, uid, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        isLoading = false;
                        DialogUtils.closeDialog();
                        if (null != mListView) {// 停止刷新
                            mListView.onRefreshComplete();
                        }
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(mContext, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        isLoading = false;
                        DialogUtils.closeDialog();
                        if (mView == null) {
                            return;
                        }
                        if (null != mListView) {// 停止刷新
                            mListView.onRefreshComplete();
                        }
                        EntityBean infoBean = (EntityBean) result;
                        String restate = infoBean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            showView(infoBean.getBean("data"));
                            return;
                        }
                        String redes = infoBean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(getContext(), redes);
                        }
                    }
                });
    }

    private void showView(EntityBean dataBean) {
        if (dataBean == null) {
            mListView.setEmptyView(emptyIV);
            return;
        }
        String counts = dataBean.getString("counts");
        if (TextUtils.isEmpty(counts)) {
            counts = "0";
        }
        countTV.setText(counts);
        sCountTV.setText(dataBean.getString("sumdegree"));
        pCountTV.setText(dataBean.getString("summoney"));
        EntityBean[] beans = dataBean.getBeans("bean");
        if (beans == null || beans.length < 1) {
            mListView.setEmptyView(emptyIV);
        } else {
            emptyIV.setVisibility(View.GONE);
        }
        if (null != mAdapter) {
            mAdapter.setData(beans);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        if (mListView != null) {
            mListView.setAdapter(null);
            mListView = null;
        }
    }
}
